package java_io.csv;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;

import static java.nio.file.StandardCopyOption.*;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;

public class WriteCSV {

	public static void main(String[] args) {
		final String dir = System.getProperty("user.dir");
		Charset charset = Charset.forName("US-ASCII");
		
		Path path = Paths.get(dir + "\\bin\\javacore\\io\\contacts.csv");
		
		ArrayList<Contact> contacts = new ArrayList<Contact>();
		contacts = ReadCSV.readFile();
		contacts.add(new Contact("test@temp.com","First name", "Last name","15/01/1970"));
		
		try (BufferedWriter writer = Files.newBufferedWriter(path, charset)) {
			String s;
			for (Contact c: contacts){
				s = c.getContact() + "\n";
				writer.write(s, 0, s.length());
			}
			
		} catch (IOException x) {
		    System.err.format("IOException: %s%n", x);
		}
		
		System.out.println("File generated:  " + path.toString());

	}

}
